import { _decorator, AudioSource, Component } from 'cc';
const { ccclass, property } = _decorator;

/**
 * 音效管理器
 */
@ccclass('AudioManager')
export class AudioManager extends Component {
    static Instance: AudioManager;

    @property({ type: AudioSource, tooltip: "鞭炮背景音乐" })
    private bgm: AudioSource = null!;

    @property(AudioSource) sfx_click: AudioSource = null!;
    @property(AudioSource) sfx_levelup: AudioSource = null!;
    @property(AudioSource) sfx_common_explosion: AudioSource = null!;
    @property(AudioSource) sfx_common_fire: AudioSource = null!;
    @property(AudioSource) sfx_fortress_cast: AudioSource = null!;
    @property(AudioSource) sfx_victory: AudioSource = null!;
    @property(AudioSource) sfx_reward: AudioSource = null!;
    @property(AudioSource) sfx_goldCollect: AudioSource = null!;
    @property(AudioSource) sfx_warnning: AudioSource = null!;

    /** 是否允许播放 */
    @property
    public canPlay: boolean = false;

    onLoad() {
        AudioManager.Instance = this;
    }

    start() {
        // this.playBgm(true);
    }

    /** 播放背景音乐 */
    public playBgm(isPlay: boolean = true) {
        if (!this.canPlay) {
            this.bgm.stop();
            return;
        }
        isPlay ? this.bgm.play() : this.bgm.stop();
    }

    public playUIClick() {
        if (!this.canPlay) return;
        this.sfx_click.play();
    }

    public playLevelUp() {
        if (!this.canPlay) return;
        this.sfx_levelup.play();
    }

    public playCommonExplosion() {
        if (!this.canPlay) return;
        this.sfx_common_explosion.play();
    }

    public playCommonFire() {
        if (!this.canPlay) return;
        this.sfx_common_fire.play();
    }

    public playFortressCast() {
        if (!this.canPlay) return;
        this.sfx_fortress_cast.play();
    }

    public playVictory() {
        if (!this.canPlay) return;
        this.sfx_victory.play();
    }

    public playGoldCollect() {
        if (!this.canPlay) return;
        this.sfx_goldCollect.playOneShot(this.sfx_goldCollect.clip, 1);
    }

    public playReward() {
        if (!this.canPlay) return;
        this.sfx_reward.play();
    }

    public playWarring() {
        if (!this.canPlay) return;
        this.sfx_warnning.play();
    }


}

